﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using WebApplication5.Model;

namespace WebApplication5.Controller
{
    public class userstoriesCtr
    {

        dmaa0912DannyEntities2 db = new dmaa0912DannyEntities2();

            public Userstory GetUserstory(int id)
            {
                var q = (from h in db.Userstory
                         where h.id == id
                         select h);

                return q.FirstOrDefault();
            }

            public void AddUserstory(Userstory u)
            {
                validationOfUserstory(u);
                db.Userstory.Add(u);
                db.SaveChanges();
            }

            public void editUserstory(Userstory UserStoryUpdate)
            {
                validationOfUserstory(UserStoryUpdate);
                Userstory userstory = db.Userstory.FirstOrDefault(h => h.id == UserStoryUpdate.id);
                userstory.actual = UserStoryUpdate.actual;
                userstory.asA = UserStoryUpdate.asA;
                userstory.Estimate = UserStoryUpdate.Estimate;
                userstory.iWantTo = UserStoryUpdate.iWantTo;
                userstory.priority = UserStoryUpdate.priority;
                userstory.Project = UserStoryUpdate.Project;
                userstory.projectId = UserStoryUpdate.projectId;
                userstory.soThat = UserStoryUpdate.soThat;
                userstory.Task = UserStoryUpdate.Task;
                userstory.name = UserStoryUpdate.name;

                db.SaveChanges();
            }

            public List<Userstory> GetAllUserstories()
            {
                try
                {
                    var userstories = from u in db.Userstory
                                   select u;

                    return userstories.ToList();
                }
                catch (Exception e)
                {

                }
                return null;
            }


            public void deleteUserstory(int id)
            {
                Userstory u = (from v in db.Userstory
                             where v.id == id
                             select v).FirstOrDefault();

                db.Userstory.Remove(u);

                db.SaveChanges();
            }

            public List<Userstory> GetAllUserstoryId(int id)
            {
                try
                {
                    var userstories = from u in db.Userstory
                                   where id == u.projectId
                                   select u;

                    return userstories.ToList();
                }
                catch (Exception e)
                {

                }
                return null;
            }

            internal List<Userstory> GetAllUserstoryIdSame(int id)
            {
                try
                {
                    var userstories = from u in db.Userstory
                                      where id == u.id
                                      select u;

                    return userstories.ToList();
                }
                catch (Exception e)
                {

                }
                return null;
            }


            private void validationOfUserstory(Userstory u)
            {

                Regex onlyDigits = new Regex("^[0-9]+$");
                Regex onlyLetters = new Regex(@"^[a-zA-Z]+$");
                Regex onlyLettersAndNumbers = new Regex(@"[a-zæøå 0-9]+");

                if (!onlyDigits.IsMatch(u.actual.ToString()))
                    throw new Exception("Kun tal tilladt i aktuel tid");

                if (string.IsNullOrEmpty(u.asA))
                    throw new Exception("Indtast kun bogstaver i As a");

                if (string.IsNullOrEmpty(u.iWantTo))
                    throw new Exception("Indtast kun bogstaver i I want to");

                if (!onlyDigits.IsMatch(u.Estimate.ToString()))
                    throw new Exception("Kun tal tilladt i estimeret tid");

                if (string.IsNullOrEmpty(u.name))
                    throw new Exception("Indtast kun bogstaver i navn");

                if (!onlyDigits.IsMatch(u.priority.ToString()))
                    throw new Exception("Kun tal tilladt i prioritet tid");

                if (string.IsNullOrEmpty(u.soThat))
                    throw new Exception("Indtast kun bogstaver i So that");

            }
    }
    
}